 package handling.world;

 import client.MapleCharacter;
 import handling.Auction.AuctionServer;
 import handling.cashshop.CashShopServer;
 import handling.channel.ChannelServer;
 import handling.channel.PlayerStorage;
 import handling.world.family.MapleFamily;
 import handling.world.family.MapleFamilyCharacter;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;









 public class WorldFamilyService
 {
/*  26 */   private static final Logger log = LogManager.getLogger(WorldFamilyService.class.getName());
   private final Map<Integer, MapleFamily> familieList;
   private final ReentrantReadWriteLock lock;

   private WorldFamilyService() {
/*  31 */     log.info("正在启动[WorldFamilyService]");
/*  32 */     this.lock = new ReentrantReadWriteLock();
/*  33 */     this.familieList = new LinkedHashMap<>();
   }

   public static WorldFamilyService getInstance() {
/*  37 */     return SingletonHolder.instance;
   }

   public void addLoadedFamily(MapleFamily family) {
/*  41 */     if (family.isProper()) {
/*  42 */       this.familieList.put(Integer.valueOf(family.getId()), family);
     }
   }

   public MapleFamily getFamily(int id) {
/*  47 */     MapleFamily ret = null;
/*  48 */     this.lock.readLock().lock();
     try {
/*  50 */       ret = this.familieList.get(Integer.valueOf(id));
     } finally {
/*  52 */       this.lock.readLock().unlock();
     }
/*  54 */     if (ret == null) {
/*  55 */       this.lock.writeLock().lock();
       try {
/*  57 */         ret = new MapleFamily(id);
/*  58 */         if (ret == null || ret.getId() <= 0 || !ret.isProper()) {
/*  59 */           return null;
         }
/*  61 */         this.familieList.put(Integer.valueOf(id), ret);
       } finally {
/*  63 */         this.lock.writeLock().unlock();
       }
     }
/*  66 */     return ret;
   }

   public void memberFamilyUpdate(MapleFamilyCharacter familyMember, MapleCharacter chr) {
/*  70 */     MapleFamily family = getFamily(familyMember.getFamilyId());
/*  71 */     if (family != null) {
/*  72 */       family.memberLevelJobUpdate(chr);
     }
   }

   public void setFamilyMemberOnline(MapleFamilyCharacter familyMember, boolean isOnline, int channel) {
/*  77 */     MapleFamily family = getFamily(familyMember.getFamilyId());
/*  78 */     if (family != null) {
/*  79 */       family.setOnline(familyMember.getId(), isOnline, channel);
     }
   }

   public int setRep(int familyId, int chrId, int addrep, int oldLevel, String oldName) {
/*  84 */     MapleFamily family = getFamily(familyId);
/*  85 */     if (family != null) {
/*  86 */       return family.setRep(chrId, addrep, oldLevel, oldName);
     }
/*  88 */     return 0;
   }

   public void save() {
/*  92 */     System.out.println("正在保存学院数据...");
/*  93 */     this.lock.writeLock().lock();
     try {
/*  95 */       for (MapleFamily family : this.familieList.values()) {
/*  96 */         family.writeToDB(false);
       }
     } finally {
/*  99 */       this.lock.writeLock().unlock();
     }
   }

   public void setFamily(int familyId, int seniorId, int junior1, int junior2, int currentrep, int totalrep, int chrId) {
/* 104 */     int ch = WorldFindService.getInstance().findChannel(chrId);
/* 105 */     if (ch == -1) {
       return;
     }

/* 109 */     MapleCharacter player = getStorage(ch).getCharacterById(chrId);
/* 110 */     if (player == null) {
       return;
     }
/* 113 */     boolean isDifferent = (player.getFamilyId() != familyId || player.getSeniorId() != seniorId || player.getJunior1() != junior1 || player.getJunior2() != junior2);
/* 114 */     player.setFamily(familyId, seniorId, junior1, junior2);
/* 115 */     player.setCurrentRep(currentrep);
/* 116 */     player.setTotalRep(totalrep);
/* 117 */     if (isDifferent) {
/* 118 */       player.saveFamilyStatus();
     }
   }

   public void familyPacket(int familyId, byte[] message, int chrId) {
/* 123 */     MapleFamily family = getFamily(familyId);
/* 124 */     if (family != null) {
/* 125 */       family.broadcast(message, -1, family.getMFC(chrId).getPedigree());
     }
   }

   public void disbandFamily(int familyId) {
/* 130 */     MapleFamily family = getFamily(familyId);
/* 131 */     if (family != null) {
/* 132 */       this.lock.writeLock().lock();
       try {
/* 134 */         this.familieList.remove(Integer.valueOf(familyId));
       } finally {
/* 136 */         this.lock.writeLock().unlock();
       }
/* 138 */       family.disbandFamily();
     }
   }

   public PlayerStorage getStorage(int channel) {
/* 143 */     if (channel == -20)
/* 144 */       return AuctionServer.getPlayerStorage(); 
/* 145 */     if (channel == -10) {
/* 146 */       return CashShopServer.getPlayerStorage();
     }
/* 148 */     return ChannelServer.getInstance(channel).getPlayerStorage();
   }

   private static class SingletonHolder
   {
/* 153 */     protected static final WorldFamilyService instance = new WorldFamilyService();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\WorldFamilyService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */